<template>
    <div>
        <el-form :inline="true" :model="formInline" class="demo-form-inline" style="border-bottom:1px solid #ccc;">
            <!--备件小类-->
            <el-form-item :label="nowLangue.spareSubClass" class="selectMB10">
                <el-select size="mini" clearable :placeholder="nowLangue.pleaseImport" v-model="formInline.subClassCode">
                    <el-option v-for="(subClass,index) in subClassList" :key="index" :label="subClass.name" :value="subClass.code"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="selectMB10">
                <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="queryLine()" v-text="nowLangue.query"></el-button>
            </el-form-item>
        </el-form>
        <!--表格-->
        <!--备件参数明细-->
        <label class="default-label" v-text="nowLangue.spareParameterList"></label>
        <!--新增按钮-->
        <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="createNew()" v-text="nowLangue.create"></el-button>
        <el-table :data="productData" border style="width: 100%;" v-loading.body="loading" :element-loading-text="nowLangue.loadDesperately">
            <el-table-column type="index" header-align="center" align="center" :label="nowLangue.sequence" width="40"></el-table-column>
            <el-table-column :label="nowLangue.operate" header-align="center" align="center" width="100">
                <template scope="scope">
                    <div class="operationItem bolder" v-if="scope.row.status" @click="handleStatus(scope.$index,scope.row)" v-text="nowLangue.forbidden"></div>
                    <div class="operationItem bolder" v-else @click="handleStatus(scope.$index,scope.row)" v-text="nowLangue.enable"></div>
                    <div class="operationItem disabled bolder" v-if="scope.row.status" v-text="nowLangue.update"></div>
                    <div class="operationItem bolder" v-else @click="updateCorFact(scope.$index,scope.row)" v-text="nowLangue.update"></div>
                </template>
            </el-table-column>
            <!--备件小类-->
            <el-table-column prop="subClassName" :label="nowLangue.spareSubClass" header-align="center" show-overflow-tooltip></el-table-column>
            <!--参数名称-->
            <el-table-column prop="name" :label="nowLangue.parameterName" header-align="center" show-overflow-tooltip></el-table-column>
            <!--参数类型-->
            <el-table-column prop="typeName" :label="nowLangue.parameterType" header-align="center"  show-overflow-tooltip></el-table-column>
            <!--参数单位-->
            <el-table-column prop="unit" :label="nowLangue.paramterUnit" header-align="center" show-overflow-tooltip></el-table-column>
            <!--创建人-->
            <el-table-column prop="creatorName" :label="nowLangue.founder" header-align="center"  show-overflow-tooltip></el-table-column>
            <!--创建时间-->
            <el-table-column prop="createTime" :label="nowLangue.creationTime" header-align="center"  show-overflow-tooltip width="160"></el-table-column>
            <!--修改人-->
            <el-table-column prop="updaterName" :label="nowLangue.modifier" header-align="center"  show-overflow-tooltip ></el-table-column>
            <!--修改时间-->
            <el-table-column prop="updateTime" :label="nowLangue.modificationTime" header-align="center"  show-overflow-tooltip width="160"></el-table-column>
        </el-table>
        <el-pagination v-if="isPaging"  @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage3" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>
        <!--新建弹窗-->
        <el-dialog :title="nowLangue.addSpareValue" :visible.sync="dialogForm1Visible" size="tiny" :lock-scroll="false">
            <el-form :inline="true" :model="addLineForm" class="demo-form-inline label-minW95" style="border-bottom:1px solid #ccc;" ref="addLineForm"> <!--  :rules="rules" -->
                <!--参数编码-->
                <el-form-item :label="nowLangue.parameterCode" prop="code" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-input v-model="addLineForm.code" :placeholder="nowLangue.pleaseImport"></el-input>
                </el-form-item>
                <!--备件小类-->
                <el-form-item :label="nowLangue.spareSubClass" prop="subClassCode" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-select size="mini" clearable :placeholder="nowLangue.pleaseImport" v-model="addLineForm.subClassCode" @change="getSubClassName">
                        <el-option v-for="(subClass,index) in subClassList" :key="index" :label="subClass.name" :value="subClass.code"></el-option>
                    </el-select>
                </el-form-item>
                <!--参数名称-->
                <el-form-item :label="nowLangue.parameterName" prop="name" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-input v-model="addLineForm.name" :placeholder="nowLangue.pleaseImport"></el-input>
                </el-form-item>
                <!--参数类型-->
                <el-form-item :label="nowLangue.parameterType" prop="type" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-select size="mini" clearable :placeholder="nowLangue.pleaseSelect" v-model="addLineForm.type">
                        <el-option v-for="(type,index) in parmTypeList" :key="index" :label="type.label" :value="type.value"></el-option>
                    </el-select>
                </el-form-item>
                <!--参数单位-->
                <el-form-item :label="nowLangue.paramterUnit" prop="unit" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-input v-model="addLineForm.unit"  :placeholder="nowLangue.pleaseImport"></el-input>
                </el-form-item>
                <!--状态-->
                <el-form-item :label="nowLangue.status" prop="status" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-radio-group v-model="addLineForm.status">
                        <el-radio :label="true" v-cloak>{{nowLangue.enable}}</el-radio>
                        <el-radio :label="false" v-cloak>{{nowLangue.forbidden}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button class="bolder outsideBtnBS" size="mini" :disabled="saveDisabled" type="danger" @click="saveTaxCodeTax" v-text="nowLangue.sure"></el-button>
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="cancleCorFact" v-text="nowLangue.cancel"></el-button>
            </div>
        </el-dialog>
        <!--修改弹窗-->
        <el-dialog :title="nowLangue.updateSpareValue" :visible.sync="dialogForm2Visible" size="tiny" :lock-scroll="false">
            <el-form :inline="true" :model="updateLineForm" class="demo-form-inline label-minW95" style="margin-bottom:20px;border-bottom:1px solid #ccc;" ref="updateLineForm">
                <!--参数编码-->
                <el-form-item :label="nowLangue.parameterCode" prop="code" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-input v-model="updateLineForm.code" :placeholder="nowLangue.pleaseImport"></el-input>
                </el-form-item>
                <!--备件小类-->
                <el-form-item :label="nowLangue.spareSubClass" prop="subClassCode" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-select size="mini" clearable :placeholder="nowLangue.pleaseImport" v-model="updateLineForm.subClassCode" @change="getSubClassName(1)">
                        <el-option v-for="(subClass,index) in subClassList" :key="index" :label="subClass.name" :value="subClass.code"></el-option>
                    </el-select>
                </el-form-item>
                <!--参数名称-->
                <el-form-item :label="nowLangue.parameterName" prop="name" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-input v-model="updateLineForm.name" :placeholder="nowLangue.pleaseImport"></el-input>
                </el-form-item>
                <!--参数类型-->
                <el-form-item :label="nowLangue.parameterType" prop="type" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-select size="mini" clearable :placeholder="nowLangue.pleaseSelect" v-model="updateLineForm.type">
                        <el-option v-for="(type1,index) in parmTypeList" :key="index" :label="type1.label" :value="type1.value"></el-option>
                    </el-select>
                </el-form-item>
                <!--参数单位-->
                <el-form-item :label="nowLangue.paramterUnit" prop="unit" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-input v-model="updateLineForm.unit"  :placeholder="nowLangue.pleaseImport"></el-input>
                </el-form-item>
                <!--状态-->
                <el-form-item :label="nowLangue.status" prop="status" :rules="[{ required: true, message: nowLangue.pleaseImport}]">
                    <el-radio-group v-model="updateLineForm.status">
                        <el-radio :label="true" v-cloak>{{nowLangue.enable}}</el-radio>
                        <el-radio :label="false" v-cloak>{{nowLangue.forbidden}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button class="bolder outsideBtnBS" :disabled="updateDisabled" size="mini" type="danger" @click="saveUpdateTaxCodeTax" v-text="nowLangue.sure"></el-button>
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="cancleUpdateCorFact" v-text="nowLangue.cancel"></el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                languageScoped:{
                    zh:{
                        "pleaseSelect":"请选择",
                        "pleaseImport":"请输入",
                        "query":"查询",
                        "spareSubClass":"备件小类",
                        "beijian":"备件",
                        "parameterName":"参数名称",
                        "parameterCode":"参数编码",
                        "spareParameterList":"备件参数明细",
                        "create":"新增",
                        "sequence":"序",
                        "operate":"操作",
                        "forbidden":"禁用",
                        "enable":"启用",
                        "update":"修改",
                        "parameterType":"参数类型",
                        "paramterUnit":"参数单位",
                        "founder":"创建人",
                        "creationTime":"创建时间",
                        "modifier":"修改人",
                        "modificationTime":"修改时间",
                        "parameterValue":"参数值",
                        "spareParameterValue":"备件参数值",
                        "status":"状态",
                        "save":"保存",
                        "cancel":"取消",
                        "loadDesperately":"拼命加载中",
                        "sure":"确定",
                        "addSpareValue":"新增备件参数",
                        "updateSpareValue":"修改备件参数",
                    },
                    en:{
                        "legalpserson":"法人管理",
                        "gaoxiaolong":"高小龙",
                        "maguangyi":"法人管理",
                        "pleaseSelect":"please Select",
                        "pleaseImport":"please Import",
                        "query":"query",
                        "spareSubClass":"spare SubClass",
                        "parameterName":"parameter Name",
                        "parameterCode":"parameter Code",
                        "spareParameterList":"spare Parameter List",
                        "create":"create",
                        "sequence":"sequence",
                        "operate":"operate",
                        "forbidden":"forbidden",
                        "enable":"enable",
                        "update":"update",
                        "parameterType":"parameter Type",
                        "paramterUnit":"paramter Unit",
                        "founder":"founder",
                        "creationTime":"creation Time",
                        "modifier":"modifier",
                        "modificationTime":"modification Time",
                        "parameterValue":"parameter Value",
                        "spareParameterValue":"spare Parameter Value",
                        "status":"status",
                        "save":"save",
                        "cancel":"cancel",
                        "loadDesperately":"load Desperately",
                        "sure":"sure",
                        "addSpareValue":"add Spare Value",
                        "updateSpareValue":"update Spare Value",

                    }
                },
                nowLangue:{},
                // 查询表单
                formInline:{
                    subClassCode:"",
                    subClassName:''
                },
                subClassList:[],//备件小类集合
                parmTypeList:[],//参数类型集合
                addLineForm:{
                    code:'',
                    name:'',
                    type:'',
                    unit:'',
                    subClassCode:'',
                    subClassName:'',
                    status:true
                },
                updateLineForm:{
                    id:'',
                    code:'',
                    name:'',
                    type:'',
                    unit:'',
                    subClassCode:'',
                    subClassName:'',
                    status:true
                },
                productData:[
                    {

                        time:'2017-08-23 00:00:00',
                        createStaff:'小刘',
                        status:true,
                        statusName:"启用"

                    },
                    {

                        time:'2017-08-23 00:00:00',
                        createStaff:'小孙',
                        status:false,
                        statusName:"禁用"
                    }
                ],//备件参数列表
                loading:false,
                disabled:false,
                //弹窗
                dialogForm1Visible:false,//新增
                dialogForm2Visible:false,//修改

                //按钮禁用，防止连点
                selectDisabled:false,
                saveDisabled:false,
                updateDisabled:false,
                // 分页
                isPaging:true,    //是否显示分页
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                currentPage:1,    //当前显示3页
                currentPage3:3,
                totalNumber: 1,     //总条数
                totalPage:1    //总页数
            };
        },
        created:function(){
            this.languageType=="zh"?this.nowLangue=this.languageScoped.zh:this.nowLangue=this.languageScoped.en;
        },
        computed:{
            languageType(){
                return this.$store.state.languageType;
            }
        },
        watch:{
            languageType(){
                this.languageType=="zh"?this.nowLangue=this.languageScoped.zh:this.nowLangue=this.languageScoped.en;
            }
        },
        //进入页面立即运行此方法
        mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'SpareParameterSetting');
            this.querySpareSmall();
            this.querySpareType();
            this.queryLine();
        },
        methods:{
            //备件小类查询
            querySpareSmall(){
                var _this=this;
                $.ajax({
                    type: 'GET',
                    url: _this.metadata_service+"subClass/findSubClassList",
                    cache: false,
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.subClassList=data.data;//备件小类集合
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                });
            },
            //参数类型查询
            querySpareType(){
                var _this=this;
                var tmp=["materialProperty.materialPropertyType"]
                $.ajax({
                    type: 'POST',
                    url: _this.metadata_service+"metadata/getPullDownList",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.parmTypeList=data.data['materialProperty.materialPropertyType'];
                        }else{
                            _this.common.errorTip(data.errorMessage)
                        }
                    }
                });
            },
            //取备件小类名称
            getSubClassName(parm){
                for(var i=0;i<this.subClassList.length;i++){
                    if(parm==1){
                        if(this.updateLineForm.subClassCode==this.subClassList[i].code){
                            this.updateLineForm.subClassName=this.subClassList[i].name;
                        }
                    }else{
                        if(this.addLineForm.subClassCode==this.subClassList[i].code){
                            this.addLineForm.subClassName=this.subClassList[i].name;
                        }
                    }
                }
            },
            //查询
            queryLine(){
                var _this=this;
                 _this.loading=true;
                _this.selectDisabled=true;
                 var tmp={
                     taxCode:_this.formInline.taxCode,
                     pageIndex:_this.pageIndex,
                     pageSize:_this.pageSize
                 };
                 $.ajax({
                     type: 'POST',
                     url: _this.metadata_service+"materialProperty/queryPage",
                     cache: false,
                     data:JSON.stringify(tmp),
                     dataType:'JSON',
                     contentType: "application/json;charset=utf-8",
                     success: function (data) {
                         _this.selectDisabled=false;
                         _this.loading=false;
                         if (data.success) {
                             _this.isPaging=true;
                             _this.totalPage=data.data.totalPage;
                             _this.totalNumber=data.data.totalNumber;
                             _this.productData=data.data.dataList;
                         }else{
                            _this.common.errorTip(data.errorMessage)
                         }
                     },
                     error:function(){
                         _this.selectDisabled=false;
                     }
                 });
            },
            //新增备件参数
            createNew(){
                this.dialogForm1Visible=true;
            },
            //新建保存
            saveTaxCodeTax(){
                var _this=this;
                 _this.$refs["addLineForm"].validate((valid) => {
                     if (valid) {
                         _this.saveDisabled=true;
                         var tmp={
                             code:_this.addLineForm.code,
                             name:_this.addLineForm.name,
                             type:_this.addLineForm.type,
                             unit:_this.addLineForm.unit,
                             subClassCode:_this.addLineForm.subClassCode,
                             subClassName:_this.addLineForm.subClassName,
                             status:_this.addLineForm.status,
                         };
                         $.ajax({
                             type: 'POST',
                             url: _this.metadata_service+"materialProperty/add",
                             cache: false,
                             data:JSON.stringify(tmp),
                             dataType:'JSON',
                             contentType: "application/json;charset=utf-8",
                             success: function (data) {
                                 if (data.success) {
                                     _this.saveDisabled=false;
                                     var successMessage=_this.languageType=="zh"?'保存成功！':'Save Success';
                                     _this.common.successTip(successMessage);
                                     _this.dialogForm1Visible=false;
                                     _this.queryLine();
                                 }else{
                                     _this.saveDisabled=false;
                                    _this.common.errorTip(data.errorMessage);
                                 }
                             },
                             error:function(){
                                 _this.saveDisabled=false;
                             }
                         });
                     }else {
                         var err=_this.languageType=="zh"?'存在未填写的必填项！':'Have Empty';
                         _this.common.errorTip(err);
                         return false;
                     }
                 });
            },
            //新建取消
            cancleCorFact(){
                var _this=this;
                _this.dialogForm1Visible=false;
                if(this.$refs['addLineForm']) {
                    this.$refs['addLineForm'].resetFields();
                }
                 _this.addLineForm.code="";
                 _this.addLineForm.name="";
                 _this.addLineForm.type="";
                 _this.addLineForm.unit="";
                 _this.addLineForm.subClassCode="";
                 _this.addLineForm.subClassName="";
                 _this.addLineForm.status=false;
            },
            // 操作启用禁用
            handleStatus(index, row){
                var _this=this;
                 if(row.status){
                    row.status = false;
                 }else{
                    row.status = true;
                 }
                 var tmp={id: row.id,status: row.status};
                 $.ajax({
                     type: 'POST',
                     url: _this.metadata_service+"materialProperty/updateStatus",
                     cache: false,
                     data:JSON.stringify(tmp),
                     dataType:'JSON',
                     contentType: "application/json;charset=utf-8",
                     success: function (data) {
                         if (data.success) {
                             // 当前状态的显示
                             if(row.status){
                                 row.statusName = _this.languageType=="zh"?' 启用':'Open';
                                 var successMessage1=_this.languageType=="zh"?'启用成功！':'Open Success';
                                 _this.common.successTip(successMessage1);
                             }else{
                                 row.statusName = _this.languageType=="zh"?' 禁用':'Close';
                                 var successMessage2=_this.languageType=="zh"?'禁用成功！':'Close Success';
                                 _this.common.successTip(successMessage2);
                             }
                         }else{
                                _this.common.errorTip(data.errorMessage);
                        }
                     }
                 });
            },
            //点击修改
            updateCorFact(index,row){
                var _this=this;
                _this.dialogForm2Visible=true;
                if(this.$refs['updateLineForm']) {
                    this.$refs['updateLineForm'].resetFields();
                }
                 _this.updateLineForm.id = row.id;
                 _this.updateLineForm.code = row.code;
                 _this.updateLineForm.name = row.name;
                 _this.updateLineForm.type = ""+row.type;
                 _this.updateLineForm.unit = row.unit;
                 _this.updateLineForm.subClassCode = row.subClassCode;
                 _this.updateLineForm.subClassName = row.subClassName;
                 _this.updateLineForm.status = row.status;
            },
            //修改保存
            saveUpdateTaxCodeTax(){
                var _this=this;
                 _this.$refs["updateLineForm"].validate((valid) => {
                     if (valid) {
                         _this.updateDisabled=true;
                         var tmp={
                             id:_this.updateLineForm.id,
                             code:_this.updateLineForm.code,
                             name:_this.updateLineForm.name,
                             type:_this.updateLineForm.type,
                             unit:_this.updateLineForm.unit,
                             subClassCode:_this.updateLineForm.subClassCode,
                             subClassName:_this.updateLineForm.subClassName,
                             status:_this.updateLineForm.status,
                         };
                         $.ajax({
                             type: 'POST',
                             url: _this.metadata_service+"materialProperty/update",
                             cache: false,
                             data:JSON.stringify(tmp),
                             dataType:'JSON',
                             contentType: "application/json;charset=utf-8",
                             success: function (data) {
                                 _this.updateDisabled=false;
                                 if (data.success) {
                                     var successMessage=_this.languageType=="zh"?'修改成功！':'Update Success';
                                     _this.common.successTip(successMessage);
                                     _this.dialogForm2Visible=false;
                                     _this.queryLine();
                                 }else{
                                     _this.common.errorTip(data.errorMessage);
                                 }
                             },
                             error:function(){
                                 _this.updateDisabled=false;
                             }
                         });
                     } else {
                         var err=_this.languageType=="zh"?'存在未填写的必填项！':'Have Empty';
                         _this.common.errorTip(err);
                         return false;
                     }
                 });
            },
            //修改取消
            cancleUpdateCorFact(){
                var _this=this;
                _this.dialogForm2Visible=false;
            },
            //分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.queryLine();
            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                this.pageIndex=val;
                this.queryLine();
            }
        }
    }
</script>



